//
//  WoViewController.m
//  B段项目
//
//  Created by dllo on 15/11/2.
//  Copyright © 2015年 徐茂怀. All rights reserved.
//

#import "WoViewController.h"
#import "FitHeader.h"
#import "NSXCollectViewController.h"
#import "NSXAbloutViewController.h"
#define HEIGHT 270.0f
#define WIDTH 390
@interface WoViewController ()<UITableViewDataSource , UITableViewDelegate>
@property(nonatomic , retain)UITableView *tableview;
@property(nonatomic , assign)NSInteger flag;
@property(nonatomic , retain)UILabel *label;
@property(nonatomic , retain)UIView *neightView;
@property(nonatomic , retain)NSArray *array;
@property(nonatomic , retain)UIImageView *imageView;
@property(nonatomic , assign)NSInteger countNumber;@end

@implementation WoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"我的";
    
    //上方的图
    _imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0 *FitWidth, -HEIGHT,375 *FitWidth, HEIGHT - 50 *FitHeight)];
    _imageView.image= [ UIImage imageNamed:@"动态图.png"];
    _imageView.contentMode = UIViewContentModeScaleAspectFill;
    [self.tableview addSubview:_imageView];
}

-(void)loadView
{
    [super loadView];
    self.array = @[@"我的收藏", @"清理缓存", @"意见反馈", @"关于"];
    self.tableview = [[UITableView alloc]initWithFrame:CGRectMake(0 *FitWidth, 0 *FitHeight, 375 *FitWidth, (603 - 49) *FitHeight) style:UITableViewStylePlain];
    self.tableview.contentInset = UIEdgeInsetsMake(HEIGHT *FitHeight, 0 *FitWidth, 0 *FitHeight, 0 *FitHeight);
    _tableview.dataSource = self;
    _tableview.delegate= self;
   
    [self.view addSubview:_tableview];
    _tableview.backgroundColor = [UIColor colorWithRed:0.89 green:1 blue:1 alpha:1];
    //清除多余的cell
    _tableview.tableFooterView = [[UIView alloc]init];
}

#pragma mark  动图判断部分
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat y = scrollView.contentOffset.y - 120 *FitHeight;
    if (y < -HEIGHT *FitHeight) {
        CGRect frame = self.imageView.frame;
        frame.origin.y = y *FitHeight;
        frame.size.height = -y *FitHeight;
        self.imageView.frame = frame;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
     static NSString *identifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
    }
    //取消点击效果
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    if (indexPath.row == 0)
    {
        cell.textLabel.text = _array[indexPath.row];
        cell.backgroundColor = [UIColor colorWithRed:0.89 green:1 blue:1 alpha:1];
    }
    if (indexPath.row == 1)
    {
        NSString *path = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES).lastObject;
        NSString *cachePath = [path stringByAppendingPathComponent:@"Caches"];
        cell.detailTextLabel.text = [NSString stringWithFormat:@"%.2fM",[self folderSizeAtPath:cachePath]];
        cell.detailTextLabel.textColor = [UIColor blackColor];
        cell.textLabel.text = self.array[indexPath.row];
        cell.textLabel.textColor = [UIColor blackColor];
        cell.backgroundColor = [UIColor colorWithRed:0.89 green:1 blue:1 alpha:1];
    }
    if (indexPath.row == 2)
    {
        cell.textLabel.text = _array[indexPath.row];
        cell.backgroundColor = [UIColor colorWithRed:0.89 green:1 blue:1 alpha:1];
    }
    if (indexPath.row == 3)
    {
        cell.textLabel.text = _array[indexPath.row];
        cell.backgroundColor = [UIColor colorWithRed:0.89 green:1 blue:1 alpha:1];
    }
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 70;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _array.count;
}

#pragma mark  缓存计算 . button的点击方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 1)
    {
        //alertView
        UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"温馨提示"message:@"清理完成"preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {}];
        [alert addAction:defaultAction];
        [self presentViewController:alert animated:YES completion:nil];
        
            /*
                     获取沙盒路径
            */
            NSString *path = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES).lastObject;
            path = [path stringByAppendingPathComponent:@"Caches"];
            NSFileManager *manager = [NSFileManager defaultManager];
            if ([manager fileExistsAtPath:path])
            {
                NSArray *fileNameArray = [manager subpathsAtPath:path];
                for (NSString *fileName in fileNameArray)
                {
                    //拼接绝对路径
                    NSString *absolutePath = [path stringByAppendingPathComponent:fileName];
                    //通过文件管理者删除文件
                    [manager removeItemAtPath:absolutePath error:nil];
                }
            }
            [_tableview reloadData];
        };
    if (indexPath.row == 0)
    {
        NSXCollectViewController *NCVC = [[NSXCollectViewController alloc]init];
        [self.navigationController pushViewController:NCVC animated:YES];
    }
    if (indexPath.row == 3)
    {
        NSXAbloutViewController *NAVC = [[NSXAbloutViewController alloc]init];
        [self.navigationController pushViewController:NAVC animated:YES];
    }
}

/*
 清理缓存部分
 */
#warning 计算单个文件夹大小的方法
-(long long)fileSizeAtPath:(NSString *)path
{
    //创建一个文件管理者
    NSFileManager *manager = [NSFileManager defaultManager];
    if ([manager fileExistsAtPath:path])
    {
        return [[manager attributesOfItemAtPath:path error:nil] fileSize];
    }
    else
    {
        return 0;
    }
}

#warning 计算一个文件夹大小
-(float)folderSizeAtPath:(NSString *)path
{
    //创建文件管理者
    NSFileManager *manager = [NSFileManager defaultManager];
    if (![manager fileExistsAtPath:path]) {
        return 0;
    }
    //根据路径获取文件夹米面的元素集合
    //获取集合类型的枚举器
    NSEnumerator *enumerator = [[manager subpathsAtPath:path] objectEnumerator];
    //每一次遍历得到的文件名
    NSString *fileNaem = [NSString string];
    //文件夹大小
    float folderSize = 0;
    while ((fileNaem = [enumerator nextObject]) != nil) {
        NSString *absolutePath = [path stringByAppendingPathComponent:fileNaem];
        folderSize += [self fileSizeAtPath:absolutePath];
    }
    //计算缓存的数据,将其转换为M
    return folderSize / (1024.0 * 1024.0);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
